#!/bin/bash

set -e

if [[ -n "${RASTER_VISION_DEBUG}" ]]; then
    set -x
fi

function usage() {
    echo -n \
         "Usage: $(basename "$0")
Setup infrastructure resources required to support GPU development.
"
}

PROJECT_ROOT="$(dirname "$(dirname "$(readlink -f "${0}")")")"

if [ "${BASH_SOURCE[0]}" = "${0}" ]
then
    if [ "${1:-}" = "--help" ]
    then
        usage
    else
        pushd "$(dirname "$0")/../deployment/terraform"

        case "${1}" in
            destroy)
                terraform "$@"
                ;;
            taint)
                terraform "$@"
                ;;
            plan)
                terraform "$@" -out="gpu-worker.tfplan"
                ;;
            apply)
                terraform apply "gpu-worker.tfplan"
                ;;
            start)
                NB_INSTANCES=${2:-1}

                "$PROJECT_ROOT/scripts/infra" plan \
                -var fleet_target_capacity="$NB_INSTANCES" \
                -var s3_bucket="$S3_BUCKET"

                "$PROJECT_ROOT/scripts/infra" apply

                # Wait for it to be ready, then get the hostname
                echo "Waiting for the EC2 instance to finish initializing"
                aws ec2 wait instance-status-ok --filter \
                            'Name=instance-state-code,Values=0,16'

                EC2_HOSTNAMES="$(aws ec2 describe-instances \
                    --filter "Name=instance-state-code,Values=16" \
                    --filter "Name=instance-type,Values=p2.xlarge" \
                    --query 'Reservations[].Instances[].PublicDnsName' --output text)"

                echo "HOSTNAMES: $EC2_HOSTNAMES"
                echo "Remember to run './scripts/infra destroy' when done!"
                ;;
            *)
                echo "ERROR: I don't have support for that Terraform subcommand!"
                exit 1
                ;;
        esac

        popd
    fi
fi
